package ch.ethz.inf.vs.sunspot;


import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import ch.ethz.inf.vs.sunspot.commons.JerseyConstants;
import ch.ethz.inf.vs.sunspot.drivercore.SpotCommunicationBackend;

/**
 * The main Launcher for this program, handles configuration and starts
 * the Driver and the Restlet
 * 
 * @author Simon Mayer, ETH Zurich
 */
public class DriverStarter {

	// Logging instance
	private Logger logService = Logger.getLogger(this.getClass().getSimpleName());
	SpotCommunicationBackend spotDriver = null;

	/**
	 * Launch the Driver
	 */
	DriverStarter() {		
		spotDriver = new SpotCommunicationBackend();
		logService.log(Level.SEVERE, spotDriver.getClass().getSimpleName() + " has been launched...");
	}	

	/**
	 * Start the driver.
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println("----- Starting Config -----");
		
		String osName = System.getProperty("os.name");
		if (osName.contains("Linux")) Constants.BASESTATION_PREFIX = "/dev/ttyACM";
		else if (osName.contains("Windows")) Constants.BASESTATION_PREFIX = "COM";
		else {
			System.out.println("The Sun SPOT Driver was unable to recognize your operating system [os.name = " + osName + "] - Exiting!");
			System.exit(0);
		}

		Constants.BASESTATION_PORT = null;
		Constants.SPOT_HOST_PORT = null;
		Constants.AUTOCONF = null;

		for (int i = 0; i < args.length; i++) {
			if (args[i].equalsIgnoreCase(Constants.ARG_AUTOCONF)) {
				Constants.AUTOCONF = true;
				System.out.println("AUTOCONF for spot host port active");				
			}
			else if (args[i].contains(Constants.ARG_BASESTATION_PORT)) {
				Constants.BASESTATION_PORT = Integer.parseInt((args[i].split("=")[1]));
				System.out.println("Setting Basestation port to " + Constants.BASESTATION_PORT);
			}
			else if (args[i].contains(Constants.ARG_SPOTHOSTPORT)) {
				Constants.SPOT_HOST_PORT = Integer.parseInt((args[i].split("=")[1]));
				System.out.println("Setting spot host port to " + Constants.SPOT_HOST_PORT);
			}
			else if (args[i].contains(Constants.ARG_SERVERPORT)) {
				JerseyConstants.SERVER_PORT = Integer.parseInt((args[i].split("=")[1]));
				System.out.println("Setting server port to " + JerseyConstants.SERVER_PORT);
			}
			else if (args[i].equalsIgnoreCase(Constants.ARG_HELP)) {
				System.out.println("Usage: java -jar sunspotdriver.jar\n" +
						"\t[" + Constants.ARG_HELP + "]\n" +
						"\t[" + Constants.ARG_BASESTATION_PORT + "SerialPort]\n" +
						"\t[" + Constants.ARG_SPOTHOSTPORT + "SpotHostPort]\n" +
						"\t[" + Constants.ARG_SERVERPORT + "ServerPort]\n" +
						"\t[" + Constants.ARG_AUTOCONF +"]");
				System.exit(0);
			}			
			else System.err.println("Parameter " + args[i] + " is invalid.");
		}

		if (Constants.BASESTATION_PORT == null) {
			Constants.BASESTATION_PORT = 0;
			System.out.println("Using basestation serial port " + Constants.BASESTATION_PREFIX + Constants.BASESTATION_PORT);
		}

		if (Constants.SPOT_HOST_PORT == null) {
			Constants.SPOT_HOST_PORT = 100;
			System.out.println("Using spot host radiogram port " + Constants.SPOT_HOST_PORT);
		}
		
		if (JerseyConstants.SERVER_PORT == null) {
			JerseyConstants.SERVER_PORT = 8080;
			System.out.println("Using server port " + JerseyConstants.SERVER_PORT);
		}

		if (Constants.AUTOCONF == null) {
			Constants.AUTOCONF = false;
			System.out.println("Switching off spot host port AUTOCONF");	
		}

		System.out.println("----- Config Done -----");
		System.out.println();
		
		new DriverStarter();
		try {
			new JerseyMain();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}